package com.keming.hander;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

//拦截有Controller注解的控制器
@ControllerAdvice
public class ControllerExceptionHandler {
//    使用指定类初始化日志对象
//    在日志输出的时候，可以打印出日志信息所在类
    private final Logger logger=LoggerFactory.getLogger(this.getClass());
    @ExceptionHandler(Exception.class)
    ModelAndView exceptionHander(HttpServletRequest request,Exception e) throws Exception {
        logger.error("Requset URL : {},Exception : {}",request.getRequestURI(),e);
        //        当标识了状态码的时候就不拦截
        if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class)!=null){
            throw e;
        }

        ModelAndView mv = new ModelAndView();
        mv.addObject("url",request.getRequestURI());
        mv.addObject("exception",e);
        mv.setViewName("error/error");
        return mv;



    }

}
